package Top_Interview_Questions_Review._006Hash;

import java.util.Arrays;

/**
 * @Author: 吕庆龙
 * @Date: 2020/1/21 19:05
 * <p>
 * 功能描述:
 */
public class _0242 {

    public static void main(String[] args) {
        _0242 test = new _0242();
        String s = "anagram";
        String t = "nagaram";
        System.out.println(test.isAnagram2(s, t));
    }


    //哈希表
    public boolean isAnagram2(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        int[] counter = new int[26];
        for (int i = 0; i < s.length(); i++) {
            counter[s.charAt(i) - 'a']++;
            counter[t.charAt(i) - 'a']--;
        }
        for (int count : counter) {
            if (count != 0) {
                return false;
            }
        }
        return true;
    }



    //不是很聪明的排序
    public boolean isAnagram1(String s, String t) {
        char[] char1 = s.toCharArray();
        char[] char2 = t.toCharArray();
        Arrays.sort(char1);
        Arrays.sort(char2);
        return Arrays.equals(char1,char2);
    }
}
